package genalgoritm;

import genalgoritm.extension.NamesUtils;
import genalgoritm.factories.ChoiseFactory;
import genalgoritm.factories.CrossoverFactory;
import genalgoritm.factories.EndFactory;
import genalgoritm.factories.MutationFactory;
import genalgoritm.managers.ChoiseManager;
import genalgoritm.managers.CrossoverManager;
import genalgoritm.managers.EndManager;
import genalgoritm.managers.MutationManager;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.InvalidRegistryObjectException;
import org.eclipse.core.runtime.Platform;

public class ExtensionUtils implements NamesUtils {

	public static void findExtensions() {
		findSelectionExtensions();
		findCrossoverExtensions();
		findMutationExtensions();
		findStoppingExtensions();
	}

	private static void findSelectionExtensions() {
		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint point = registry.getExtensionPoint(SELECTION_EXTENSION);
		if (point == null)
			return;
		IExtension[] extensions = point.getExtensions();
		for (int i = 0; i < extensions.length; i++) {
			for (IConfigurationElement el : extensions[i]
					.getConfigurationElements()) {
				Class composite;
				try {
					String cName = el.getAttribute(COMPOSITE);
					if (cName == null || cName.length() == 0) {
						composite = null;
					} else {
						composite = Class.forName(el.getAttribute(COMPOSITE));
					}
				} catch (InvalidRegistryObjectException e) {
					composite = null;
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					composite = null;
					e.printStackTrace();
				}
				try {
					ChoiseFactory f = new ChoiseFactory(el.getAttribute(NAME),
							Class.forName(el.getAttribute(POLICY)),
							(composite == null) ? null : composite, el
									.getAttribute(DESCRIPTION));
					ChoiseManager.getInstance().add(f);
				} catch (InvalidRegistryObjectException e) {
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private static void findCrossoverExtensions() {
		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint point = registry.getExtensionPoint(CROSSOVER_EXTENSION);
		if (point == null)
			return;
		IExtension[] extensions = point.getExtensions();
		for (int i = 0; i < extensions.length; i++) {
			for (IConfigurationElement el : extensions[i]
					.getConfigurationElements()) {
				Class composite;
				try {
					String cName = el.getAttribute(COMPOSITE);
					if (cName == null || cName.length() == 0) {
						composite = null;
					} else {
						composite = Class.forName(el.getAttribute(COMPOSITE));
					}
				} catch (InvalidRegistryObjectException e) {
					composite = null;
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					composite = null;
					e.printStackTrace();
				}
				try {
					CrossoverFactory f = new CrossoverFactory(el
							.getAttribute(NAME), Class.forName(el
							.getAttribute(POLICY)), (composite == null) ? null
							: composite, el.getAttribute(DESCRIPTION));
					CrossoverManager.getInstance().add(f);
				} catch (InvalidRegistryObjectException e) {
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private static void findMutationExtensions() {
		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint point = registry.getExtensionPoint(MUTATION_EXTENSION);
		if (point == null)
			return;
		IExtension[] extensions = point.getExtensions();
		for (int i = 0; i < extensions.length; i++) {
			for (IConfigurationElement el : extensions[i]
					.getConfigurationElements()) {
				Class composite;
				try {
					String cName = el.getAttribute(COMPOSITE);
					if (cName == null || cName.length() == 0) {
						composite = null;
					} else {
						composite = Class.forName(el.getAttribute(COMPOSITE));
					}
				} catch (InvalidRegistryObjectException e) {
					composite = null;
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					composite = null;
					e.printStackTrace();
				}
				try {
					MutationFactory f = new MutationFactory(el
							.getAttribute(NAME), Class.forName(el
							.getAttribute(POLICY)), (composite == null) ? null
							: composite, el.getAttribute(DESCRIPTION));
					MutationManager.getInstance().add(f);
				} catch (InvalidRegistryObjectException e) {
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private static void findStoppingExtensions() {
		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint point = registry.getExtensionPoint(STOPPING_EXTENSION);
		if (point == null)
			return;
		IExtension[] extensions = point.getExtensions();
		for (int i = 0; i < extensions.length; i++) {
			for (IConfigurationElement el : extensions[i]
					.getConfigurationElements()) {
				Class composite;
				try {
					String cName = el.getAttribute(COMPOSITE);
					if (cName == null || cName.length() == 0) {
						composite = null;
					} else {
						composite = Class.forName(el.getAttribute(COMPOSITE));
					}
				} catch (InvalidRegistryObjectException e) {
					composite = null;
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					composite = null;
					e.printStackTrace();
				}
				try {
					EndFactory f = new EndFactory(el.getAttribute(NAME), Class
							.forName(el.getAttribute(POLICY)),
							(composite == null) ? null : composite, el
									.getAttribute(DESCRIPTION));
					EndManager.getInstance().add(f);
				} catch (InvalidRegistryObjectException e) {
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
